#####
# Plotting Code Immigration Outcome - Crossing Borders Project
#####

#Make sure to load 03analysis_v5.R and 04political_effects_plot before starting this code. Comment the following out if not done already
#source("03analysis_v5.R")
#source("04political_effects_plot.R")

# Event study plot --------------------------------------------------------
dfESplot <- extractPlotInfoDT(mod_df_es_weights_98, coef_extract = names(coef(mod_df_es_98)))

# add the 1999 row
dfESplot <- rbindlist(list(
  dfESplot,
  data.table(y = NA, est_name = "border15_1999", est = 0, lower95 = 0, upper95 = 0, lower90 = 0, upper90 = 0)
))

# set the x-axis values as year
set(dfESplot, NULL, "y", value = as.integer(substring(dfESplot$est_name, 10, 13)))

# indicator for period
dfESplot[ , period := factor(fcase(
  y < 2000, "Pre-reform period",
  y %in% 2000:2003, "Transition period",
  y > 2003, "Free movement period"
), levels = c("Pre-reform period", "Transition period", "Free movement period"))]

dfPlotL <- ggplot(dfESplot, aes(x = y, y = est, ymin = lower95, ymax = upper95, color = period, shape = period)) +
  geom_linerange() +
  geom_point() +
  scale_color_manual(values = cbPalette[c(8,2,1)], name = NULL) +
  scale_shape_manual(values = 18:16, name = NULL) +
  scale_x_continuous(limits = c(1995.5, 2016.5), expand = expansion(0,0), breaks = seq(1996, 2016, 4)) +
  scale_y_continuous(breaks = seq(0,12,4), expand = expansion(0, .07)) +
  ylab("Effects of open borders on immigrant workers per 100 Swiss residents") +
  xlab(NULL) +
  ggtitle("A. Annual effects of border opening") +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(size = 9),
    axis.text = element_text(size = 7),
    legend.text = element_text(size = 7),
    axis.title = element_text(size = 7)
  ) 


# Effect by region ---------------------------------------------------------------
mods <-
  rbindlist(
    list(
      "All regions" = dforiginMods[weights == "popJanTotal" & grepl("98", outcome)],
      "German language region" = dforiginModsDE[weights == "popJanTotal" & grepl("98", outcome)],
      "French language region" = dforiginModsFR[weights == "popJanTotal" & grepl("98", outcome)],
      "Italian and Romansh language regions" = dforiginModsIT[weights == "popJanTotal" & grepl("98", outcome)]
    ),
    idcol = T
  )

set(mods, NULL, "weights", NULL)

plotMat <- mods[ , extractPlotInfoDT(reg), by = .(.id, outcome)]

set(plotMat, NULL, "y", rep(1:2, times = nrow(plotMat)/2))

set(plotMat, NULL, j = "df_group", value = fcase(
  plotMat$outcome == "df98", "All",
  plotMat$outcome == "dfDE98", "Germany",
  plotMat$outcome == "dfFR98", "France",
  plotMat$outcome == "dfIT98", "Italy",
  plotMat$outcome == "dfOE98", "Austria"
))

set(plotMat, NULL, j = "df_group", value = factor(plotMat$df_group, levels = c("All", "Germany", "France", "Italy", "Austria")))
set(plotMat, NULL, ".id", factor(plotMat$.id, levels = c("All regions", "German language region", "French language region", "Italian and Romansh language regions")))

# attempt at four axes, one per region
dfPlotR <- ggplot(plotMat[df_group %in% c("All", "Germany", "France", "Italy")], aes(y = est, ymax = upper95, ymin = lower95, x = df_group, color = factor(y == 1), shape = factor(y == 1))) +
  geom_linerange(position = position_dodge(-.25)) +
  geom_point(position = position_dodge(-.25)) +
  facet_wrap( ~ .id , scales = "free", ncol = 1) +
  scale_y_continuous( minor_breaks = NULL, expand = expansion(.1, 0)) +
  scale_color_manual(values = cbPalette[-3], name = NULL, labels = c("Free movement", "Transition period")) +
  scale_shape_manual(values = c(16, 17), name = NULL, labels = c("Free movement", "Transition period")) +
  xlab("Origin countries of immigrant workers") +
  ylab("Effects of open borders on immigrant workers per 100 Swiss residents") +
  ggtitle("B. Effects by region and origin of immigrant workers") +
  theme_minimal() +
  theme(legend.position = "bottom",
        axis.ticks.length.y = unit(0, "pt"),
        strip.text = element_text(hjust = 0, size = 7),
        axis.text = element_text(size = 7),
        axis.title = element_text(size = 7),
        axis.title.x = element_text(vjust = 0, size = 7),
        legend.margin = margin(0,0,0,0),
        legend.text = element_text(size = 7),
        legend.box.margin = margin(-5,0,0,0,"pt"),
        plot.margin = margin(0,0,0,10, "pt"),
        plot.title = element_text(size = 9),
        legend.key.size = unit(.75, "line"))


# Combined plot -----------------------------------------------------------
dfOut <- (
  dfPlotL + theme(text = element_text(family = "Times New Roman")) +
    dfPlotR + theme(text = element_text(family = "Times New Roman"), legend.position = "none") + 
    plot_layout(
      ncol = 2,
      widths = c(4.5/8, 3.5/8)
    )
) / guide_area() +
  plot_layout(
    guides = "collect",
    nrow = 2,
    heights = c(9.75/10, .25/10)
  ) + plot_annotation(theme = theme(plot.margin = margin(-1,-1,0,-1)))


ggsave(filename = here("figures", "Fig2.png"), width = 8, height = 8 * .65, dpi = 600)


